﻿using AutoMapper;
using HongYeSystemWebApi.Templet.Entity.Entities;
using HongYeSystemWebApi.Templet.Entity.Request;
using HongYeSystemWebApi.Templet.Entity.Request.SysProject;
using HongYeSystemWebApi.Templet.Entity.Request.SysSupplier;
using HongYeSystemWebApi.Templet.Entity.Response;
using HongYeSystemWebApi.Templet.Entity.Response.Project;
using HongYeSystemWebApi.Templet.Entity.Response.SysSupplier;
using HongYeSystemWebApi.Templet.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace HongYeSystemWebApi.Templet.Service
{
    public class SupplierService : ISupplierService
    {

        private readonly SqlSugarClient _db;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        /// <param name="db"></param>
        public SupplierService(IHelpDB db, IMapper mapper)
        {
            this._db = db.OpenDB();
            _mapper = mapper;
        }
        public async Task DeleteAsync(MultiIdRequest request)
        {
            List<Supplier> DelData = new List<Supplier>();
            foreach (var item in request.Ids)
            {
                var project = await _db.Queryable<Supplier>().FirstAsync(a => a.Id == item);
                project.Delete = true;
                project.OperatDate = DateTime.Now;
                DelData.Add(project);
            }
            _db.Updateable(DelData).WhereColumns(it => new { it.Id }).ExecuteCommand();
        }
      
        public async Task InsertAsync(SysSupplierInsertRequest request)
        {
            var data = _mapper.Map<Supplier>(request);
            data.OperatDate = DateTime.Now;
            var dataFir = await _db.Queryable<Supplier>().FirstAsync(a => !a.Delete && a.Name == data.Name);
            if (dataFir != null)
            {
                throw new Exception("存在相同名称的数据，请修改名称后再次添加");
            }
            await _db.Insertable(data).ExecuteCommandAsync();
        }

        public async Task UpdateAsync(SysSupplierUpdataRequest request)
        {
            var dataFir = await _db.Queryable<Supplier>().FirstAsync(X => X.Id == request.Id);
            if (dataFir == null)
            {
                throw new Exception("未找到修改原始数据");
            }
            var data = _mapper.Map<Supplier>(request);
            data.OperatDate = DateTime.Now;
            await _db.Updateable(data).WhereColumns(it => new { it.Id }).ExecuteCommandAsync();
        }

        public async Task<PageModel<SysSupplierDataPageResponse>> GetListAsync(KeyPageRequest request)
        {
            var data = _db.Queryable<Supplier>().Where(a => !a.Delete);
            if (!string.IsNullOrEmpty(request.key))
            {
                data = data.Where(a => (a.Name.Contains(request.key) || a.Number.Contains(request.key)));
            } 
            RefAsync<int> Total = 0;
            var resultpage = await data.OrderBy(x => x.Id, OrderByType.Desc).ToPageListAsync(request.PageNumber, request.PageSize, Total);
            var ResultData = _mapper.Map<List<SysSupplierDataPageResponse>>(resultpage);
            return new PageModel<SysSupplierDataPageResponse>() { Data = ResultData, Total = Total };
        }
    }
}
